<template>
  <div class="invoice_add">
    <van-sticky>
      <van-nav-bar title="新增收货地址" left-arrow @click-left="back" />
    </van-sticky>
    <van-cell-group>
      <van-field v-model="form.realName" label="收件人" placeholder="收件人" />
      <van-field v-model="form.cellPhone" label="手机号码" placeholder="手机号码" />
      <van-field v-model="addressxl" label="所在地区" placeholder="所在地区" readonly @click='cityTreeTag=true' />
      <van-field v-model="form.address" label="详细地址" placeholder="详细地址" />
	
      <van-field v-model="form.email" label="邮箱" placeholder="邮箱" />
    </van-cell-group>
    <div class="defalut row-between-center">
      <label for="">设为默认地址</label>
      <van-switch v-model="form.deft" active-value="1" inactive-value="0" size="20" />
    </div>
	<van-popup v-model="cityTreeTag" position="bottom">
		<van-area value="" columns-num='2' :area-list="areaList" @confirm="sure" @cancel='move' cancel-button-text='清除' />
	</van-popup>
    <!-- <p class="tips">温馨提示请填写完整准确信息，以便开具增值税发票下次开票您只要出示上述信息即可，我们不会把您的信息用作其他用途</p> -->
    <div class="btn" @click="addInvoice">添加</div>
  </div>
</template>

<script>
import api from 'api/api'
import city from '../../assets/city.js'
export default {
  data () {
    return {
      chosenAddressId: '1',
	  areaList: {},
	  cityTreeTag: false,
	  addressxl:'',
      form: {
        realName: '',
        cellPhone: '',
        area: '',
        address: '',
        email: '',
        deft: 0
      }
    }
  },
  created() {
  	this.areaList = city
  },
  methods: {
    addInvoice () {
		var form=this.form;
      if (form.realName== "" || form.cellPhone == "" || form.area=="" || form.address=="" || form.email=="") {
        this.$toast('请填写完整表单')
        return
      }
	  else if(!(/^1[3456789]\d{9}$/.test(form.cellPhone))){
		  this.$toast('手机号填写有误')
		  return
	  }
	  else if(!(/^\w+@[a-z0-9]+\.[a-z]+$/i.test(form.email))){
		  this.$toast('邮箱输入有误')
		  return
	  }
      api.addressUpdate({
        data: this.form
      }).then(res => {
        this.$toast('添加成功')
        this.back()
      })
    },
	sure(value) {
				console.log(value)
				this.addressxl = value[0].name + '-' + value[1].name
				this.form.area = value[1].code

				this.cityTreeTag = false
			},
			move() {
				this.cityTreeTag = false
				this.addressxl = ''
				this.form.area = ""

			},
    back () {
      window.history.go(-1)
    }
  }
}
</script>

<style lang="scss">
.invoice_add {
  min-height: 100vh;
  background-color: #fff;
  text-align: left;
  .btn {
    width: 6.88rem;
    height: 0.91rem;
    line-height: 0.91rem;
    text-align: center;
    background: linear-gradient(
      96deg,
      rgba(72, 143, 240, 1),
      rgba(51, 130, 239, 1)
    );
    border-radius: 0.45rem;
    margin: 0 auto;
    color: #fff;
    font-size: 0.36rem;
  }
  .defalut {
    font-size: 0.4rem;
    color: #999;
    padding: 0.3rem;
    label {
      font-size: 0.3rem;
    }
  }
  .tips {
    font-size: 0.22rem;
    padding: 0.3rem;
  }
  .mb_18 {
    margin-bottom: 0.18rem;
  }
  .mb_105 {
    margin-bottom: 1.05rem;
  }
}
</style>